package com.zuberot.apnmacedonia;

import com.zuberot.apnmacedonia.operators.MobileOperator;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;

/**
 * @author Viktor Stojanovski - (zuberot@gmail.com)
 *
 */
public class APNFuntions {

	// URIs
	public static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
	public static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

	public static boolean setDefaultAPN(Context context, int operatorId) {
		boolean res = false;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.put("apn_id", operatorId);
		try {
			resolver.update(PREFERRED_APN_URI, values, null, null);
			Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name",	"apn" }, "_id=" + operatorId, null, null);
			if (c != null) {
				res = true;
				c.close();
			}
		} catch (SQLException e) {
			Log.d(APNMacedoniaActivity.logTag,
					"setDefaultAPN: " + e.getMessage());
		}
		return res;
	}

	public static int insertAPNInternet(Context context, MobileOperator mobileOperator) {
		int id = -1;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(MobileOperator.OPERATOR_APN_NAME,mobileOperator.getInternetApnName());
		values.put(MobileOperator.OPERATOR_APN, mobileOperator.getInternetApn());
		values.put(MobileOperator.OPERATOR_USER, mobileOperator.getInternetUser());
		values.put(MobileOperator.OPERATOR_PASSWORD, mobileOperator.getInternetPassword());
		values.put(MobileOperator.OPERATOR_MCC, mobileOperator.getMcc());
		values.put(MobileOperator.OPERATOR_MNC, mobileOperator.getMnc());
		values.put(MobileOperator.OPERATOR_NUMERIC, mobileOperator.getNumeric());
		values.put(MobileOperator.OPERATOR_TYPE, mobileOperator.getInternetApnType());
		Cursor c = null;
		try {
			Uri newRow = resolver.insert(APN_TABLE_URI, values);
			if (newRow != null) {
				c = resolver.query(newRow, null, null, null, null);
				int idindex = c.getColumnIndex("_id");
				c.moveToFirst();
				id = c.getShort(idindex);

			}
		} catch (SQLException e) {
			Log.d(APNMacedoniaActivity.logTag, e.getMessage());
		}

		if (c != null)
			c.close();
		return id;
	}

	public static int insertAPNMMS(Context context, MobileOperator mobileOperator) {
		int id = 0;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(MobileOperator.OPERATOR_APN_NAME, mobileOperator.getMmsApnName());
		values.put(MobileOperator.OPERATOR_APN, mobileOperator.getMmsApn());
		values.put(MobileOperator.OPERATOR_USER, mobileOperator.getMmsUser());
		values.put(MobileOperator.OPERATOR_PASSWORD,mobileOperator.getMmsPassword());
		values.put(MobileOperator.OPERATOR_MMSC, mobileOperator.getMmsMMSC());
		values.put(MobileOperator.OPERATOR_MMSPROXY,mobileOperator.getMmsProxy());
		values.put(MobileOperator.OPERATOR_MMSPORT, mobileOperator.getMmsPort());
		values.put(MobileOperator.OPERATOR_MCC, mobileOperator.getMcc());
		values.put(MobileOperator.OPERATOR_MNC, mobileOperator.getMnc());
		values.put(MobileOperator.OPERATOR_NUMERIC, mobileOperator.getNumeric());
		values.put(MobileOperator.OPERATOR_TYPE, mobileOperator.getMmsApnType());
		Cursor c = null;
		try {
			Uri newRow = resolver.insert(APN_TABLE_URI, values);
			if (newRow != null) {
				c = resolver.query(newRow, null, null, null, null);
				int idindex = c.getColumnIndex("_id");
				c.moveToFirst();
				id = c.getShort(idindex);

			}
		} catch (SQLException e) {
			Log.d("TAG", e.getMessage());
		}

		if (c != null)
			c.close();

		return id;
	}

}
